<?php

    //验证session
    session_start();
    if(!isset($_SESSION['admin'])){
        echo '<script>alert("请先登录");location.href="index.php";</script>';
        exit;
    }

    //连接数据库
    include("../config/config.php");
    //获取用户数据
    $user=$dbh->query("SELECT * FROM user")->fetchAll();

?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="/static/jquery-1.10.2.min.js"></script>
    <title>用户编辑</title>
</head>
<style>
    table{
        width: 1000px;
        margin:0 auto;
        border-collapse: collapse;
    }
    table th,table td{
        border:solid 1px black;
        text-align: center;
    }
    td input{
        width:98%;
        height: 100%;
        border: none;
        text-align: center;
    }
</style>
<body>
    <div>
        <div style="    height: 30px;">
            <div style="float: right;"><button onclick="admin_logout()">退出登录</button></div>
        </div>

        <!--  -->
        <div style="text-align: center;">
            会员管理
        </div>
        <table>
            <tr>
                <th>用户名</th>
                <th>昵称</th>
                <th>修改</th>
                <th>删除</th>
            </tr>
            <?php

                foreach ($user as  $value) {
                    echo '
                <tr>
                    <td><input type="text" id="username'.$value['id'].'" value="'.$value['username'].'"></td>
                    <td><input type="text" id="name'.$value['id'].'" value="'.$value['name'].'"></td>
                    <td><button onclick="modify('."'".$value['id']."'".')">修改</button></td>
                    <td><button onclick="dele('."'".$value['id']."'".')">删除</button></td>
                </tr>
                    ';
                }
            ?>
            <tr>
                <td><input type="text" id="username"></td>
                <td><input type="text" id="name"></td>
                <td></td>
                <td><button onclick="add()">新增</button></td>
            </tr>
        </table>
        <div style="    height: 30px;">
        </div>
        
       
    </div>
</body>
<script>
    function modify(id){

        if($("#username"+id).val()==""){
            alert("不能留空格");
            return false;
        }
        if(!window.confirm("确定修改数据？")){
            return false;
        }
        $.post(
            "../controler.php",
            {
                handle:"modify",
                id:id,
                username:$("#username"+id).val(),
                name:$("#name"+id).val(),
            },
            function(msg){
                if(msg=="done"){
                    location.reload();
                }
                else{
                    alert("系统出错");
                }
            }
        )
    }
    function add(){
        if($("#username").val()=="" ){
            alert("不能留空格");
            return false;
        }
        if(!window.confirm("确定增加数据？")){
            return false;
        }
        $.post(
            "../controler.php",
            {
                handle:"add",
                username:$("#username").val(),
                name:$("#name").val(),
            },
            function(msg){
                if(msg=="done"){
                    alert("新增成功，默认密码：123")
                    location.reload();
                }
                else{
                    alert("系统出错");
                }
            }
        )
    }
    function dele(id){
        if(!window.confirm("确定删除数据？")){
            return false;
        }
        $.post(
            "../controler.php",
            {
                handle:"dele",
                id:id,
            },
            function(msg){
                if(msg=="done"){
                    location.reload();
                }
                else{
                    alert("系统出错");
                }
            }
        )
    }
    function admin_logout(){
        $.post(
            "../controler.php",
            {
                handle:"admin_logout",
            },
            function(msg){
                if(msg=="done"){
                    location.href="index.php";
                }
                else{
                    alert("系统出错");
                }
            }
        )
    }

    
    

    

</script>
</html>